﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;
public partial class Manage_basisset_ClassBaseInfo : AdminPageBase
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetClassId();
        if (!IsPostBack)
        {

            BindClassType();
            BindClass();

            if (add == 1)
            {
                btnsave.Enabled = true;
            }
            else
            {
                btnsave.Enabled = false;
            }
           
        }
    }
    protected void BindClassType()
    {
        DataSet ds = jtDataAccess.GetList("ClassType", "1=1", "ClassTypeId");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlClassType.DataSource = ds;
            ddlClassType.DataTextField = "Name";
            ddlClassType.DataValueField = "ClassTypeId";
            ddlClassType.DataBind();

        }
        ListItem item = new ListItem("=请选择课程类型=", "0");
        ddlClassType.Items.Insert(0, item);
    }
    protected void GetClassId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;
          
        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
           
        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindClass()
    {
        if (id != 0)
        {
            DataSet ds = jtDataAccess.GetList("Class", "ClassId=" + id, "ClassId");
            if (CoreTools.CheckDataSet(ds))
            {
                txtcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                txtname.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                txtPassScore.Text = ds.Tables[0].Rows[0]["PassScore"].ToString();
                txttime.Text = ds.Tables[0].Rows[0]["Time"].ToString();
                lbluser.Text = ds.Tables[0].Rows[0]["UserId"].ToString();
                rbtlIsvalid.SelectedValue = ds.Tables[0].Rows[0]["Isvalid"].ToString();
                hdfuserid.Value = ds.Tables[0].Rows[0]["UserId"].ToString();
                ddlClassType.SelectedValue = ds.Tables[0].Rows[0]["ClassTypeId"].ToString();
                txtStudentsObject.Text = ds.Tables[0].Rows[0]["StudentsObject"].ToString();
                txtStudyGoals.Text = ds.Tables[0].Rows[0]["StudyGoals"].ToString();
                txtStudyMode.Text = ds.Tables[0].Rows[0]["StudyMode"].ToString();
                ViewState["musicURL"] = ds.Tables[0].Rows[0]["BackgroundMusic"].ToString();
                if (ds.Tables[0].Rows[0]["BackgroundMusic"].ToString() != "")
                {
                    string playurl = ConfigHelper.MusicUploadPath.Replace("~/", "../../");
                    lblPlay.Text = Play.PlayBackGroudMusic(playurl + ds.Tables[0].Rows[0]["BackgroundMusic"].ToString());
                    ibtnmusic.Visible = true;
                    btndelBackGroudMusic.Visible = true;
                }
            }
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string name = txtname.Text.Trim();
        string code = txtcode.Text.Trim();
        string StudentsObject = txtStudentsObject.Text;
       // string StudyGoals =CoreTools.RemoveHtml(txtStudyGoals.Text);
        string StudyGoals = txtStudyGoals.Text;
        string StudyMode = txtStudyMode.Text;
        int classTypeId =Convert.ToInt32(ddlClassType.SelectedValue);
        int isvalid = Convert.ToInt32(rbtlIsvalid.SelectedValue);
        int userid = Convert.ToInt32(AdminSessionHelper.UserDetails["UserId"]);
        decimal time = decimal.Parse(txttime.Text.Trim());
        decimal passScore = decimal.Parse(txtPassScore.Text.Trim());
        string BackgroundMusic = "";
        if (ViewState["musicURL"] != null)
            BackgroundMusic = ViewState["musicURL"].ToString();
        if (!CoreTools.ProcessSqlStr(name, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtname.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(code, 0))
        {
           ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtcode.Focus();
        }
        else
        {
            JTDataAccess.OpType OpType;
            if (id == 0)
            {
                OpType = JTDataAccess.OpType.Insert;
              
            }
            else
            {
                OpType = JTDataAccess.OpType.Update;
               
            }

            int flag = jtDataAccess.OpClass(OpType, id, classTypeId, code, name, time, userid, isvalid, passScore, StudyGoals, StudentsObject, StudyMode, BackgroundMusic);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，该编号或名称已经使用，请更换试试！"), false);
                  
                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                    break;
                default:
                   // ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", "<script>alert('操作成功！'); parent.document.getElementById('hidclassid').value=" + flag + ";</script>", false);
                    break;
            }
        }
    }
    protected void btnupload_Click(object sender, EventArgs e)
    {
        if (InputFile1.HasFile == true)
        {
            if (InputFile1.FileName != null)
            {
                if (InputFile1.FileName.Length == 0)
                {

                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请选择要上传文件"), false);

                }
                else
                {
                    string fileName = Guid.NewGuid().ToString();
                    int position = InputFile1.FileName.LastIndexOf(".");
                    if (position == -1)
                    {

                        ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("上传的文件格式不正确！"), false);
                    }
                    else
                    {
                        string OriginalName = this.InputFile1.FileName;//获取上传文件的文件名,包括后缀
                        string extensionName = InputFile1.FileName.Substring(position);
                        string filePath = "";
                        string ClassResourceType = "";
                        ClassResourceType = CoreTools.GetClassResourceType(extensionName.ToLower());
                        if (ClassResourceType.Equals("music"))
                            filePath = ConfigHelper.MusicUploadPath;
                     
                        fileName = fileName + extensionName;
                        if (filePath != "")
                        {
                            InputFile1.MoveTo(Server.MapPath(filePath + fileName), Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
                            int FileSize = int.Parse(((float)(InputFile1.ContentLength / 1024)).ToString()); //获取文件大小,单位是KB
                            ViewState["musicURL"] = fileName;
                            string playurl = ConfigHelper.MusicUploadPath.Replace("~/", "../../");
                            lblPlay.Text = Play.PlayBackGroudMusic(playurl + fileName);
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("上传成功。"), false);
                            ibtnmusic.Visible = true;
                            btndelBackGroudMusic.Visible = true;
                        }
                        else
                           // CoreTools.PageAlert("上传的文件格式不正确！");
                            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("上传的文件格式不正确！"), false);
                    }
                }
            }

        }
        else
           // CoreTools.PageAlert("请选择要上传文件");
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("请选择要上传文件"), false);
    }
    protected void ibtnmusic_Click(object sender, ImageClickEventArgs e)
    {
        if (lblPlay.Text == "")
        {
            if (ViewState["musicURL"] != null)
            {
                string fileName = ViewState["musicURL"].ToString();
                string playurl = ConfigHelper.MusicUploadPath.Replace("~/", "../../");
                lblPlay.Text = Play.PlayBackGroudMusic(playurl + fileName);
                ibtnmusic.ImageUrl = "../../images/btn_backgroudmusic.png";
            }
        }
        else
        {
            lblPlay.Text = "";
            ibtnmusic.ImageUrl = "../../images/btn_closed_music.png";
        }
    }
    protected void btndelBackGroudMusic_Click(object sender, EventArgs e)
    {
        string BackgroundMusic = "";
        if (ViewState["musicURL"] != null)
            BackgroundMusic = ViewState["musicURL"].ToString();
        CoreTools.FilePicDelete(HttpContext.Current.Request.MapPath(ConfigHelper.MusicUploadPath.Replace("~/", "../../") + BackgroundMusic));
        if (id > 0)
        {
            int flag = jtDataAccess.DelClassBackGroudMusic(id);

            if (flag == 0)
            {
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
            }
            else if (flag == 1)
            {
                ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", "<script>alert('操作成功！'); parent.document.getElementById('hidclassid').value=" + flag + ";</script>", false);
                ViewState["musicURL"] = null;
                lblPlay.Text = "";
                btndelBackGroudMusic.Visible = false;
                ibtnmusic.Visible = false;
            }
        }
        else
        {
            ViewState["musicURL"]= null;
            lblPlay.Text = "";
            btndelBackGroudMusic.Visible = false;
            ibtnmusic.Visible = false;
        }

    }
}